01 public String helloName(String name) { String s = "Hello " + name + "!"; return s; } 02 public String makeAbba(String a, String b) { String s = a + b + b + a; return s; } 03 public String makeTags(String tag, String word) { String openingTag = "<" + tag + ">"; String closingTag = ""; String s = openingTag + word + closingTag; return s; } 04 public String makeOutWord(String out, String word) { String out1 = out.substring(0,2); String out2 = out.substring(2); String s = out1 + word + out2; return s; } 05 public String extraEnd(String str) { int len = str.length(); String last2 = str.substring(len-2); String s = last2 + last2 + last2; return s; } 06 public String firstTwo(String str) { int len = str.length(); String first2 = ""; if (len < 2) { first2 = str; } if (len >= 2) { first2 = str.substring(0,2); } return first2; } 06 public String firstTwo(String str) { int len = str.length(); String first2 = str; // default if len < 2 if (len >= 2) { first2 = str.substring(0,2); } return first2; } 07 public String firstHalf(String str) { int len = str.length(); int mid = len / 2; String half1 = str.substring(0,mid); //String half2 = str.substring(mid); return half1; } 08 public String withoutEnd(String str) { int len = str.length(); String s = str.substring(1,len-1); return s; } 09 public String comboString(String a, String b) { String combo = ""; int lenA = a.length(); int lenB = b.length(); if (lenA < lenB) { combo = a + b + a; } if (lenB < lenA) { combo = b + a + b; } return combo; } 09 public String comboString(String a, String b) { String shorter = ""; String longer = ""; int lenA = a.length(); int lenB = b.length(); if (lenA < lenB) { shorter = a; longer = b; } if (lenB < lenA) { shorter = b; longer = a; } String combo = shorter + longer + shorter; return combo; } 09 public String comboString(String a, String b) { String combo = a + b + a; // default: IF a is shorter int lenA = a.length(); int lenB = b.length(); if (lenB < lenA) { combo = b + a + b; // IF b is shorter } return combo; } 09 public String comboString(String a, String b) { String shorter = a; // default String longer = b; // default int lenA = a.length(); int lenB = b.length(); if (lenB < lenA) { // IF b is shorter ! shorter = b; longer = a; } String combo = shorter + longer + shorter; return combo; } 10 public String nonStart(String a, String b) { String a1 = a.substring(1); String b1 = b.substring(1); String s = a1 + b1; return s; } 11 public String left2(String str) { String left2 = str.substring(0,2); String rest = str.substring(2); String s = rest + left2; return s; } 12 public String right2(String str) { int len = str.length(); String right2 = str.substring(len-2); String rest = str.substring(0,len-2); String s = right2 + rest; return s; } // if front is true, return the FIRST character // if front is false, (i.e. !front), return the LAST character 13 public String theEnd(String str, boolean front) { String s = ""; if (front) { s = str.substring(0,1); } if (!front) { int len = str.length(); s = str.substring(len-1); } return s; } // !front, returning the last character, is the DEFAULT 13 public String theEnd(String str, boolean front) { int len = str.length(); String s = str.substring(len-1); if (front) { s = str.substring(0,1); } return s; } // front, returning the first character, is the DEFAULT 13 public String theEnd(String str, boolean front) { String s = str.substring(0,1); if (!front) { int len = str.length(); s = str.substring(len-1); } return s; } 14 public String withouEnd2(String str) { int len = str.length(); String s = ""; if (len >= 2) { s = str.substring(1,len-1); } return s; } 15 public String middleTwo(String str) { int len = str.length(); int mid = len / 2; String s = str.substring(mid-1,mid+1); return s; } 16 public boolean endsLy(String str) { boolean ends = false; if ( str.endsWith("ly") ) { ends = true; } return ends; } 16 public boolean endsLy(String str) { boolean ends = false; int len = str.length(); if ( str.startsWith("ly",len-2) ) { ends = true; } return ends; } 16 public boolean endsLy(String str) { boolean ends = false; int len = str.length(); if (len >= 2) { String last2 = str.substring(len-2); if ( last2.equals("ly") ) { ends = true; } } return ends; } 17 public String nTwice(String str, int n) { int len = str.length(); String firstN = str.substring(0,n); String lastN = str.substring(len-n); String s = firstN + lastN; return s; } // this is a tough one! 18 public String twoChar(String str, int index) { int len = str.length(); String s = str.substring(0,2); if (0 <= index && index <= len-2) { s = str.substring(index,index+2); } return s; } 19 public String middleThree(String str) { int len = str.length(); int mid = len / 2; String s = str.substring(mid-1,mid+2); return s; } 20 public boolean hasBad(String str) { boolean bad = false; if ( str.startsWith("bad") ) { bad = true; } if ( str.startsWith("bad",1) ) { bad = true; } return bad; } 20 public boolean hasBad(String str) { boolean bad = false; if ( str.startsWith("bad") || str.startsWith("bad",1) ) { bad = true; } return bad; } // the long and inefficient way 20 public boolean hasBad(String str) { boolean bad = false; int len = str.length(); if (len >= 3) { String first3 = str.substring(0,3); if ( first3.equals("bad") ) { bad = true; } } if (len >= 4) { String second3 = str.substring(1,4); if ( second3.equals("bad") ) { bad = true; } } return bad; } 21 public String atFirst(String str) { String s = ""; int len = str.length(); if (len == 0) { s = "@@"; } if (len == 1) { s = str + "@"; } if (len >= 2) { s = str.substring(0,2); } return s; } 21 public String atFirst(String str) { String s = "@@"; // default for empty string int len = str.length(); if (len == 1) { s = str + "@"; } if (len >= 2) { s = str.substring(0,2); } return s; } 22 public String lastChars(String a, String b) { String s = ""; int lenA = a.length(); int lenB = b.length(); String firstA = "@"; String lastB = "@"; if (lenA >=1) { firstA = a.substring(0,1); } if (lenB >= 1) { lastB = b.substring(lenB-1); } s = firstA + lastB; return s; } 23 public String conCat(String a, String b) { String s = a + b; int lenA = a.length(); if (lenA >= 1) { String lastA = a.substring(lenA-1); if (b.startsWith(lastA) ) { s = a + b.substring(1); } } return s; } 23 public String conCat(String a, String b) { String s = a + b; int lenB = b.length(); if (lenB >= 1) { String firstB = b.substring(0,1); if (a.endsWith(firstB) ) { s = a + b.substring(1); } } return s; } 24 public String lastTwo(String str) { int len = str.length(); String s = str; if (len >= 2) { String last = str.substring(len-1); String secondToLast = str.substring(len-2, len-1); String rest = str.substring(0,len-2); s = rest + last + secondToLast; } return s; } 25 public String seeColor(String str) { String clr = ""; if ( str.startsWith("red") ) { clr = "red"; } if ( str.startsWith("blue") ) { clr = "blue"; } return clr; } 25 public String seeColor(String str) { final String CLR1 = "red"; final String CLR2 = "blue"; String clr = ""; if ( str.startsWith(CLR1) ) { clr = CLR1; } if ( str.startsWith(CLR2) ) { clr = CLR2; } return clr; } 26 public boolean frontAgain(String str) { boolean again = false; int len = str.length(); if (len >= 2) { String first2 = str.substring(0,2); if ( str.endsWith(first2) ) { again = true; } } return again; } 27 public String minCat(String a, String b) { String s = a + b; int lenA = a.length(); int lenB = b.length(); if (lenA != lenB) { int minLen = Math.min(lenA,lenB); String aMin = a.substring(lenA-minLen); String bMin = b.substring(lenB-minLen); s = aMin + bMin; } return s; } 27 public String minCat(String a, String b) { String s = a + b; int aLen = a.length(); int bLen = b.length(); if (aLen < bLen) { s = a + b.substring(bLen-aLen); } if (bLen < aLen) { s = a.substring(aLen-bLen) + b; } return s; } 28 public String extraFront(String str) { int len = str.length(); String s = ""; if (len < 2) { s = str + str + str; } if (len >= 2) { String front = str.substring(0,2); s = front + front + front; } return s; } 28 public String extraFront(String str) { String s = str + str + str; int len = str.length(); if (len >= 2) { String front = str.substring(0,2); s = front + front + front; } return s; } 28 public String extraFront(String str) { int len = str.length(); int stopPos = Math.min(2,len); String front = str.substring(0,stopPos); String copies3 = front + front + front; return copies3; } 29 public String without2(String str) { String s = str; int len = str.length(); if (len >= 2) { String first2 = str.substring(0,2); if (str.endsWith(first2)) { s = str.substring(2); } } return s; } // use frontAgain()!!! 29b public String without2(String str) { String s = str; if ( frontAgain(str) ) { s = str.substring(2); } return s; } 29b public boolean frontAgain(String str) { boolean again = false; int len = str.length(); if (len >= 2) { String first2 = str.substring(0,2); if ( str.endsWith(first2) ) { again = true; } } return again; } 30 public String deFront(String str) { String s = ""; String first = ""; String second = ""; String rest = str.substring(2); if (str.startsWith("a") ) { first = "a"; } if (str.startsWith("b",1) ) { second = "b"; } s = first + second + rest; return s; } 31 public String startWord(String str, String word) { String s = ""; String word1 = word.substring(1); if ( str.startsWith(word1,1) ) { int lenW = word.length(); s = str.substring(0,lenW); } return s; } 32 public String withoutX(String str) { String s = str; if (s.startsWith("x")) { // s, not str !!! s = s.substring(1); } if (s.endsWith("x")) { int len = s.length(); s = s.substring(0,len-1); } return s; } 33 public String withoutX2(String str) { String s = str; // The order matters! // You have to check the 2nd character FIRST. // If you check the 1st character FIRST // and then delete it because it's an "x", // the entire string will be shifted one position to the left. // The result: the 2nd character is now at the 1st position! // Check the 2nd character if (s.startsWith("x",1)) { s = s.substring(0,1) + s.substring(2); } // Check the 1st character if (s.startsWith("x")) { s = s.substring(1); } return s; } 33 public String withoutX2(String str) { String s = ""; String first = ""; String second = ""; String rest = ""; int len = str.length(); if (len >= 2) { rest = str.substring(2); } if (!str.startsWith("x") && len >= 1) { first = str.substring(0,1); } if (!str.startsWith("x",1) && len >= 2) { second = str.substring(1,2); } s = first + second + rest; return s; }